package com.csyf.manager.service.impl;

import com.csyf.manager.channel.base.ServiceEnum;
import com.csyf.manager.entity.*;
import com.csyf.manager.schedule.MinuteProcessSchedule;
import com.csyf.manager.service.ProxyQueryBiz;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.service.SysChannelBiz;
import com.csyf.manager.service.SysTradeTypeBiz;
import com.csyf.manager.utils.NumberHelpler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@Service
public class ProxyQueryBizImpl implements ProxyQueryBiz {



    @Autowired
    private SysAgentBalanceBiz sysAgentBalanceBiz;

    @Autowired
    private SysChannelBiz sysChannelBiz;

    @Autowired
    private SysChannelAttrBiz sysChannelAttrBiz;

    @Autowired
    private SysTradeTypeBiz sysTradeTypeBiz;

    @Autowired
    private MinuteProcessSchedule minuteProcessSchedule;

    @Autowired
    private Environment env;

    /**
     * 单商户余额查询
     * @param mchId
     * @return
     */
    @Override
    public List<BalanceShowVo> queryBalance(String mchId) {

        List<BalanceShowVo> showVos = new ArrayList<>();
        Set<String> strings = new HashSet<>();

        List<SysAgentChannel> sysAgentChannels = sysChannelBiz.queryByMchId(mchId);
        for (SysAgentChannel entry : sysAgentChannels) {
            strings.add(entry.getChannelNo());
        }

        String merchantNo = mchId;

        Iterator<String> iterator = strings.iterator();


        while (iterator.hasNext()) {
            String next = iterator.next();

            SysChannelAttr balanceNameByChanneNo = sysChannelAttrBiz.findBalanceNameByChanneNo(next);
            System.out.println(balanceNameByChanneNo.toString());
            if (null != balanceNameByChanneNo && null != balanceNameByChanneNo.getBalanceName()) {
                BalanceShowVo balance = getBalance(merchantNo, balanceNameByChanneNo.getBalanceName(), balanceNameByChanneNo.getRemark());
                showVos.add(balance);
            }
        }

        return showVos;

    }


    private BalanceShowVo getBalance(String merchantNo, String balanceName, String showname) {
        BalanceShowVo showVo = new BalanceShowVo();
        showVo.setMch_id(merchantNo);
        showVo.setChannelName(showname);

        List<SysChannelAttr> tradeByBalanceName = sysChannelAttrBiz.findTradeByBalanceName(balanceName);
        String[] trandeServices = new String[tradeByBalanceName.size()];
        for (int i = 0; i < tradeByBalanceName.size(); i++) {
            trandeServices[i] = tradeByBalanceName.get(i).getService();

        }

        SysChannelAttr noService = sysChannelAttrBiz.findNoServiceByBalanceName(balanceName);
        // 可用余额
        BigDecimal availableBalance = querybalance(merchantNo, new String[]{noService.getService()}, balanceName);
        showVo.setAvailableBalance(availableBalance);

        // 初始化垫资余额
        BigDecimal dzBalance = BigDecimal.ZERO;

        SysChannelAttr dzService = sysChannelAttrBiz.findDZServiceByBalanceName(balanceName);
        if (null == dzService) {
            showVo.setDzBlance(dzBalance);
        } else {
            dzBalance = sysAgentBalanceBiz.queryTodayProfit(merchantNo, trandeServices, new String[]{dzService.getService()}, dzService.getRate().toString());
            showVo.setDzBlance(dzBalance);
        }

        // 今日交易
        BigDecimal tradeBalance = sysAgentBalanceBiz.queryTodaySumTrade(merchantNo, trandeServices);
        showVo.setTradeBlance(tradeBalance);


        // 未清算金额
        if (dzService.getRate().intValue()==1) {
            showVo.setUnclearBalance(sysAgentBalanceBiz.queryUnclearBalance(merchantNo, balanceName));
        } else {
            showVo.setUnclearBalance(sysAgentBalanceBiz.querySomeUnclearBalance(merchantNo, trandeServices, new String[]{dzService.getService()}, dzService.getRate().toString()));
        }



        // 计算总余额
        BigDecimal totalBalance = NumberHelpler.add(availableBalance, dzBalance);

        showVo.setTotalBalance(totalBalance);

        // 冻结余额
        showVo.setFrozenBalance(sysAgentBalanceBiz.queryFrozeBalance(merchantNo, balanceName));

        return showVo;
    }


    private BigDecimal querybalance(String mchId, String[] trandeServices, String balanceName) {
        BigDecimal bigDecimal = sysAgentBalanceBiz.queryAvailableBalance(mchId, balanceName, trandeServices);
        return bigDecimal;

    }


    /**
     * 同步上游代付支付结果
     */
    @Override
    public void syncUpChannel(SysPayment sysPayment) {

        //获取当前运行环境
        String[] activeProfiles = env.getActiveProfiles();
        List<String> acts = Arrays.asList(activeProfiles);

        List<SysPayment> sysPayments = new ArrayList<>();
        sysPayments.add(sysPayment);
        minuteProcessSchedule.syncDfOrder(sysPayments, acts);
    }



}
